package com.service;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

public class test {
    private static List<String[]> list = new ArrayList<String[]>();
    static String[] finalData = new String[4];
    public static void main(String[] args) {
        String index = "(58, 391), (110, 409)";
        String []temp = new String[10];
        temp = index.split(";");

            //System.out.println(temp[i]);
            String[] temp1 = index.split("[(,)]");
            System.out.println(temp1.length);
//            temp1[0] = temp1[1];
//            temp1[1] = temp1[2];
//            temp1[2] = temp1[5];
//            temp1[3] = temp1[6];
            list.add(temp1);
        for(int i=0;i<list.size();i++){
            for(int j=0;j<list.get(i).length;j++){
                System.out.println(list.get(i)[j]);
            }
            System.out.println("======");
        }

//        //downloadFile("http://192.168.1.110:22/media/wit/Data21/Projects/yh/projects_dev/CRAI/static/img/save/page1_2021-09-15-170217.png","D:\\study\\Android\\Image");
////        DownAndReadFile("http://192.168.1.110:22/media/wit/Data21/Projects/yh/projects_dev/CRAI/static/img/save/page1_2021-09-15-170217.png");
//        DownAndReadFile("D:/study/AndroidImage\\img01.png");
    }

    public static void downloadFile(String remoteFilePath, String localFilePath) {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File f = new File(localFilePath);
        try {
            urlfile = new URL(remoteFilePath);
            httpUrl = (HttpURLConnection) urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            System.out.println("上传成功");
            bos.flush();
            bis.close();
            httpUrl.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 远程下载文件并读取返回p
     * @param filePath 文件网络地址 如http://www.baidu.com/1.txt
     * @return String
     */
    public static String DownAndReadFile(String filePath){
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        File savePath = new File("D://"+date);//创建新文件
        if (!savePath.exists()) {
            savePath.mkdir();
        }
        String[] urlname = filePath.split("/");
        int len = urlname.length-1;
        String uname = urlname[len];//获取文件名
        try {
            File file = new File(savePath+"//"+uname);//创建新文件
            if(file!=null && !file.exists()){
                file.createNewFile();
            }
            OutputStream oputstream = new FileOutputStream(file);
            URL url = new URL(filePath);
            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
            uc.connect();
            InputStream iputstream = uc.getInputStream();
            System.out.println("file size is:"+uc.getContentLength());//打印文件长度
            byte[] buffer = new byte[4*1024];
            int byteRead = -1;
            while((byteRead=(iputstream.read(buffer)))!= -1){
                oputstream.write(buffer, 0, byteRead);
            }
            oputstream.flush();
            iputstream.close();
            oputstream.close();
            //读取文件
            StringBuffer strb = new StringBuffer();
            FileInputStream fs = new FileInputStream(new File(savePath+"//"+uname));
            InputStreamReader isr = new InputStreamReader(fs,"UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String data = "";
            while((data = br.readLine()) != null){
                strb.append(data + "\n");
            }
            br.close();
            fs.close();
            isr.close();
            System.out.println(strb.toString());
            return strb.toString();

        } catch (Exception e) {
            System.out.println("读取失败！"+e);
            e.printStackTrace();
        }
        return null;
    }
}
